package com.example.administrator.testproject.downM3U8;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.example.administrator.testproject.R;
import com.hdl.m3u8.M3U8InfoManger;
import com.hdl.m3u8.bean.M3U8;
import com.hdl.m3u8.bean.OnDownloadListener;
import com.hdl.m3u8.bean.OnM3U8InfoListener;
import com.hdl.m3u8.utils.NetSpeedUtils;

public class M3U8Activity extends AppCompatActivity implements View.OnClickListener {
    //url随时可能失效
//    private String url = "http://suo.im/zAt2p1";
    private String url = "http://1255417817.vod2.myqcloud.com/c7c63ce8vodgzp1255417817/b3e97ecf4564972819119068622/playlist.m3u8";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_m3_u8);
        findViewById(R.id.check_btn).setOnClickListener(this);
        findViewById(R.id.down_btn).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.check_btn) {
            M3U8InfoManger.getInstance().getM3U8Info(url, new OnM3U8InfoListener() {
                @Override
                public void onSuccess(M3U8 m3U8) {
                    Log.e("Tag", "获取成功了" + m3U8);
                }

                @Override
                public void onStart() {
                    Log.e("Tag", "开始获取信息");
                }

                @Override
                public void onError(Throwable errorMsg) {
                    Log.e("Tag", "出错了" + errorMsg);
                }
            });
        } else if (v.getId() == R.id.down_btn) {
            onDownload();
        }

    }

    long lastLength;
    M3U8DownloadTask task1 = new M3U8DownloadTask("1001");

    //下载按钮监听事件
    public void onDownload() {
        task1.download(url, new OnDownloadListener() {
            @Override
            public void onDownloading(final long itemFileSize, final int totalTs, final int curTs) {
                Log.e("Tag", task1.getTaskId() + "下载中.....itemFileSize=" + itemFileSize + "\ttotalTs=" + totalTs + "\tcurTs=" + curTs);
            }

            /**
             * 下载成功
             */
            @Override
            public void onSuccess() {
                Log.e("Tag", task1.getTaskId() + "下载完成了");
            }

            /**
             * 当前的进度回调
             *
             * @param curLenght
             */
            @Override
            public void onProgress(final long curLenght) {
                if (curLenght - lastLength > 0) {
                    final String speed = NetSpeedUtils.getInstance().displayFileSize(curLenght - lastLength) + "/s";
                    Log.e("Tag", task1.getTaskId() + "speed = " + speed);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.e("Tag", "更新了");
                        }
                    });
                    lastLength = curLenght;

                }
            }

            @Override
            public void onStart() {
                Log.e("Tag", task1.getTaskId() + "开始下载了");
            }

            @Override
            public void onError(Throwable errorMsg) {
                Log.e("Tag", task1.getTaskId() + "出错了" + errorMsg);
            }
        });
    }

//    public static String getNativeM3u(String url, List<String> pathList, String saveFilePath) {
//        HttpResponse res = HttpConnect.getResponseFromUrl(url);
//        int num = 0;
//        //需要生成的目标buff
//        StringBuffer buf = new StringBuffer();
//        try {
//            if (res != null) {
//                InputStream in = res.getEntity.getContent;
//                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//                String line = "";
//                while ((line = reader.readLine) != null) {
//                    if (line.length > 0 && line.startsWith("http://")) {
//                        //replce 这行的内容
//                        buf.append("file:/" + saveFilePath + num + ".ts\r\n");
//                        num++;
//                    } else {
//                        buf.append(line + "\r\n");
//                    }
//                }
//                in.close;
//            }
//        } catch (Exception e) {
//            e.printStackTrace;
//        }
//        return buf.toString;
//    }
}
